package com.zhijian.warehouse.manage.service.impl;


import com.zhijian.autoconfigure.cache.CacheClient;
import com.zhijian.util.JsonUtil;
import com.zhijian.warehouse.manage.entity.dto.UserDto;
import com.zhijian.warehouse.manage.service.CacheDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CacheDaoImpl implements CacheDao {
    
    @Autowired
    private CacheClient cacheClient;
    
    /**
     * {@inheritDoc}
     */
    @Override
    public CacheClient support() {
        return cacheClient;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public UserDto getUserByToken(String key, String token) {
        if (StringUtils.isBlank(token))
            return null;
        String userJson = cacheClient.get(String.format(key, token));
        if (StringUtils.isBlank(userJson))
            return null;
        return JsonUtil.toBean(userJson, UserDto.class);
    }
}
